<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring Boot Demo</title>
</head>
<link href="static/style_bt/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="static/style_bt/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css" />
<link href="static/style_bt/bootstrap3-dialog/dist/css/bootstrap-dialog.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="static/style_bt/js/jquery.min.js"></script>
<script type="text/javascript" src="static/style_bt/js/bootstrap.min.js"></script>
<script type="text/javascript" src="static/style_bt/bootstrap3-dialog/dist/js/bootstrap-dialog.js"></script>
<body style="background-image:url(static/style_bt/rBVaEVmUD_GAeUt_AAvWFkcZHjA345.jpg);background-repeat:no-repeat;background-size: 100% auto">

	<div style="width: 70%;margin: 5% 15% 5% 20%;text-align: center">
		<form id="loginForm" class="form-horizontal">
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-6">
				<h3>用户登录</h3>
			</div>
		</div>
		
		<div class="alert alert-danger" id="errDiv" style="display: none">
			<p>Invalid username and password.</p>
		</div>
		
		<div class="alert alert-danger" id="errDiv2" style="display: none">
			<p>未知错误</p>
		</div>
		
		<div class="form-group">
			<label for="userName" class="col-sm-2 control-label">userName</label>
			<div class="col-sm-6">
				<input type="text" class="form-control" id="userName" name="userName" data-toggle="tooltip" data-placement="bottom" title="请输入用户名" placeholder="请输入用户名">
			</div>
		</div>
		<div class="form-group">
			<label for="password" class="col-sm-2 control-label">passWord</label>
			<div class="col-sm-6">
				<input type="text" class="form-control" id="password" name="password" data-toggle="tooltip" data-placement="bottom" title="请输入密码" placeholder="请输入密码">
			</div>
		</div>
		<!-- <input type="hidden" name="${_csrf.parameterName}"  value="${_csrf.token}" /> -->
		<div class="form-group">
		    <div class="col-sm-offset-2 col-sm-6">
				<div class="checkbox">
					<label>
						<input type="checkbox" id="rememberme" name="remember-me"> 记住我
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
		    <div class="col-sm-offset-2 col-sm-6">
				<button id="submit_btn" type="button" class="btn btn-default">登录</button>
				<button type="reset" class="btn btn-default">重置</button>
			</div>
		</div>
	</form>
	</div>

</body>
<script type="text/javascript">
$(function(){
	$('[data-toggle="tooltip"]').tooltip();//工具提示
	
	$('#submit_btn').click(function(){
		 $.ajax({
			url:'http://localhost:8082/ldstudy/ajaxLogin.do',
			data:JSON.stringify($("#loginForm").serializeObject()),
            contentType: "application/json;charset=utf-8",
			type:'post',
            dataType : 'json',
			success:function(obj){
			    // alert(JSON.stringify(obj));
				if(obj!=null){
				//	$.parseJSON(workJsonString);json字符串转换成json对象
				// 	var jsonObj =  eval("("+obj+")");
					if(obj.code=='0'){
                        location.href = "index.html";
					}else if(obj.code=='4000'){
						$('#errDiv').css('display','block');
					}else{
						//location.href = "login.html";
						$('#errDiv2').css('display','block');
					}
				}else{
					//location.href = "login.html";
					$('#errDiv2').css('display','block');
				}
			},
			error:function(obj){
				alert(JSON.stringify(obj));
				//alert('error');
			}
		}); 
	});
	
	/* var error = GetQueryString('error');
	alert(error);
	if(error!=null&&error!=''){
		$('#errDiv').css('display','block');
	} */

    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };


});

//根据参数名获取url中的参数值
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
</script>
</html>